<template>
  <div class="result-container">
    <div class="outter">
      <div class="inner">
        <div class="el-input el-input--small el-input--prefix">
          <input
            v-model.lazy="keyword"
            type="text"
            autocomplete="off"
            placeholder="输入关键字，点击回车键即可搜索"
            class="el-input__inner"
          />
          <span class="el-input__prefix">
            <i class="el-input__icon el-icon-search"></i>
          </span>
        </div>
        <div class="title-wrap">
          <span class="title">关键词：{{ keyword }}</span>
          <span class="sub-title">找到{{ total }}个结果</span>
        </div>
        <el-tabs v-model="activeIndex">
          <el-tab-pane label="歌曲" name="songs">
            <table class="el-table">
              <thead>
                <th class="sort"></th>
                <th>音乐标题</th>
                <th>歌手</th>
                <th>专辑</th>
                <th class="time">时长</th>
              </thead>
              <tbody>
                <tr
                  class="el-table__row"
                  v-for="(item, index) in songs"
                  :key="index"
                  @click="playmusic(item)"
                >
                  <td class="td_sort">{{ index + 1 }}</td>
                  <td>
                    <div class="song-wrap">
                      <div class="name-wrap">
                        <span>{{ item.name }}</span>
                        <span v-if="item.mvid" class="iconfont icon-mv"></span>
                      </div>
                    </div>
                  </td>
                  <td>{{ item.artists[0].name }}</td>
                  <td>{{ item.album.name }}</td>
                  <td class="td_time">{{ item.duration | formDate }}</td>
                </tr>
              </tbody>
            </table>
          </el-tab-pane>
          <el-tab-pane label="歌单" name="lists">
            <div class="recom">
              <div
                class="recom-item"
                v-for="(item, index) in lists"
                :key="index"
                @click="golist(item.id)"
              >
                <div class="imgbox">
                  <div class="dsc">
                    <i>播放量：{{ item.playCount | count }}</i>
                  </div>
                  <img :src="item.coverImgUrl" alt />
                  <span class="iconfont icon-triangle-down"></span>
                </div>
                <div class="listname">{{ item.name }}</div>
              </div>
            </div>
          </el-tab-pane>
          <el-tab-pane label="MV" name="mv">
            <div class="items">
              <div
                class="item"
                @click="toMv(item.id)"
                v-for="(item, index) in mvs"
                :key="index"
              >
                <div class="img-wrap">
                  <img :src="item.cover" alt />
                  <div class="num-wrap">
                    <div class="iconfont icon-triangle-down"></div>
                    <div class="num">{{ item.playCount | count }}</div>
                  </div>
                  <div class="iconbox">
                    <span class="iconfont icon-triangle-down"></span>
                  </div>
                </div>
                <div class="info-wrap">
                  <div class="name">{{ item.name }}</div>
                  <div class="singer">{{ item.artistName }}</div>
                </div>
              </div>
            </div>
          </el-tab-pane>
        </el-tabs>
        <div class="pagebox">
          <el-pagination
            @current-change="handleCurrentChange"
            background
            layout="prev, pager, next"
            :total="total"
            :current-page="page"
            :page-size="12"
            hide-on-single-page
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { GetSong, Search } from "../../../../api/music"
export default {
  name: "result",
  data() {
    return {
      activeIndex: "songs",
      keyword: "",
      count: 0,
      limit: 20,
      total: 0,
      type: 1,
      page: 1,
      songs: [],
      lists: [],
      mvs: [],
    }
  },
  filters: {
    formDate(date) {
      var m = parseInt(date / 1000 / 60)
      if (m < 10) {
        m = "0" + m
      }
      var s = parseInt((date / 1000) % 60)
      if (s < 10) {
        s = "0" + s
      }
      return m + ":" + s
    },
    count(playcount) {
      if (playcount >= 10000) {
        playcount = Math.ceil(playcount / 10000) + "万"
      }
      return playcount
    },
  },
  created() {
    this.keyword = this.$route.query.keyword
    this.clicksearch()
  },
  watch: {
    // 如果路由发生变化，再次执行该方法
    $route: function(newVal, oldVal) {
      this.keyword = this.$route.query.keyword
      this.clicksearch() // 搜索的方法
    },
    keyword(newVal) {
      if (newVal) {
        this.page = 1
        this.keyword = newVal
        this.clicksearch()
      }
    },
    activeIndex(newVal) {
      switch (newVal) {
        case "songs":
          this.type = 1
          this.page = 1
          this.clicksearch()
          break
        case "lists":
          this.type = 1000
          this.page = 1
          this.clicksearch()
          break
        case "mv":
          this.type = 1004
          this.page = 1
          this.clicksearch()
          break
      }
    },
  },
  methods: {
    golist(id) {
      this.$router.push(`/musichome/listinfo?id=${id}`)
    },
    toMv(id) {
      this.$router.push(`/musichome/videoinfo?id=${id}`)
    },
    handleCurrentChange(val) {
      this.page = val
      this.clicksearch()
    },
    // 搜索
    async clicksearch() {
      const res = await Search({
        keywords: this.keyword,
        type: this.type,
        limit: this.limit,
        offset: (this.page - 1) * this.limit,
      })
      if (this.type == 1) {
        this.songs = res.result.songs
        this.total = res.result.songCount
      } else if (this.type == 1000) {
        this.lists = res.result.playlists
        this.total = res.result.playlistCount
      } else {
        this.mvs = res.result.mvs
        this.total = res.result.mvCount
      }
    },
    // 点击播放
    async playmusic(item) {
      this.$store.dispatch("getSong", { id: item.id, autoplay: true })
    },
    // 移动端加载更多
    async loadMore() {
      this.page++
      const res = await Search({
        keywords: this.keyword,
        type: this.type,
        limit: this.limit,
        offset: (this.page - 1) * this.limit,
      })
      if (this.type == 1) {
        this.songs.push(...res.result.songs)
        this.total = res.result.songCount
      } else if (this.type == 1000) {
        this.lists.push(...res.result.playlists)
        this.total = res.result.playlistCount
      } else {
        this.mvs.push(...res.result.mvs)
        this.total = res.result.mvCount
      }
    },
  },
}
</script>

<style lang="scss" scoped>
@import "../../../../../static/scss/common.scss";

.result-container {
  width: 100%;
  height: calc(100% - 40px);
  margin: 20px 0px;
  user-select: none;
}
.result-container .title-wrap {
  display: flex;
  align-items: flex-end;
  margin: 20px;
  margin-bottom: 30px;
}
.el-input {
  width: calc(100% - 40px);
  margin: 0px 20px;
}
.el-tabs {
  margin: 0px 20px;
}
.result-container .title-wrap .title {
  font-weight: bold;
  margin-right: 20px;
}

.result-container .title-wrap .sub-title {
  font-size: 15px;
  color: #bebebe;
}
tr:hover {
  color: $primary;
  cursor: pointer;
}
.result-container .el-tabs__item {
  font-size: 18px;
}

.result-container .el-table td,
.result-container .el-table th.is-leaf {
  border-bottom: none;
}
.pagebox {
  margin-top: 20px;
  display: flex;
  width: 100%;
  height: 60px;
  justify-content: center;
  align-items: center;
}
.more {
  display: none;
}
.recom {
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
}
.recom-item {
  width: 20%;
  padding: 10px;
  box-sizing: border-box;
  .imgbox {
    width: 100%;
    border-radius: 5px;
    position: relative;
    overflow: hidden;
    img {
      width: 100%;
      object-fit: cover;
      border-radius: 5px;
    }
    .dsc {
      position: absolute;
      width: 100%;
      height: 30px;
      background-color: rgba(0, 0, 0, 0.5);
      top: 0px;
      left: 0px;
      transition: 0.5s;
      text-align: right;
      i {
        display: inline-block;
        width: 85%;
        color: rgb(255, 255, 255);
        font-size: 14px;
        line-height: 25px;
        font-style: normal;
        margin: 0px 10px;
      }
    }

    span {
      position: absolute;
      right: 10px;
      bottom: 10px;
      color: $primary;
      width: 35px;
      height: 35px;
      background: rgba(247, 247, 247, 0.5);
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 50%;
      opacity: 0;
      transition: 0.5s;
    }

    span:before {
      transform: translateX(1px);
    }
  }
  .imgbox:hover {
    cursor: pointer;
  }
  .imgbox:hover .dsc {
    top: 0px;
  }
  .imgbox:hover span {
    opacity: 1;
  }
  .listname {
    height: 30px;
    line-height: 30px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
.recom-item:hover span {
  opacity: 1;
}
.items {
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
}

.items .item {
  width: 25%;
  cursor: pointer;
  padding: 10px;
  box-sizing: border-box;
}

.items .item .img-wrap {
  width: 100%;
  position: relative;
  .iconbox {
    position: absolute;
    top: 0px;
    left: 0px;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    color: $primary;
    display: flex;
    justify-content: center;
    align-items: center;
    opacity: 0;
    transition: 0.5s;
    span {
      color: $primary;
      width: 35px;
      height: 35px;
      background: rgba(255, 255, 255, 0.7);
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 50%;
    }
    span:hover {
      cursor: pointer;
    }
    span:before {
      transform: translateX(1px);
    }
  }
}
.items .item .img-wrap:hover .iconbox {
  opacity: 1;
}
.items .item .img-wrap img {
  width: 100%;
  border-radius: 5px;
}

.items .item .img-wrap .num-wrap {
  position: absolute;
  color: white;
  top: 0;
  right: 0;
  display: flex;
  align-content: center;
  font-size: 15px;
  padding-right: 5px;
  padding-top: 2px;
}

.items .item .img-wrap .num-wrap .icon-triangle-down {
  font-size: 12px;
  display: flex;
  align-items: center;
  margin-right: 5px;
}

.items .item .info-wrap .name {
  height: 25px;
  line-height: 25px;
  font-size: 15px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.items .item .info-wrap .singer {
  font-size: 14px;
  height: 20px;
  line-height: 20px;
  color: #c5c5c5;
}
.outter {
  width: 100%;
  display: flex;
  justify-content: center;
  .inner {
    width: 100%;
    max-width: 1200px;
  }
}

/deep/ .el-tabs__item.is-active {
  color: $primary;
}
/deep/ .el-tabs__active-bar {
  background-color: $primary;
}
/deep/ .el-tabs__nav div:hover {
  color: $primary;
}
/deep/ .el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: $primary;
}
/deep/ .el-pagination.is-background .el-pager li:not(.disabled):hover {
  color: $primary;
}
</style>
